﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Presentation.Regions;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Samples.KMoore.WPFSamples.Zap;

namespace LaserTag.Infrastructure.RegionAdapters
{
    public class ZapScrollerRegionAdapter : RegionAdapterBase<ZapScroller>
    {
        public ZapScrollerRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {
        }

        protected override void Adapt(IRegion region, ZapScroller regionTarget)
        {
            region.ActiveViews.CollectionChanged += (sender, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (var item in e.NewItems)
                    {
                        regionTarget.CurrentItemIndex = regionTarget.Items.IndexOf(item);
                        break;
                    }
                }
            };

            region.Views.CollectionChanged += (sender, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (var item in e.NewItems)
                    {
                        regionTarget.Items.Add(item);
                    }
                }
            };
        }

        protected override IRegion CreateRegion()
        {
            return new SingleActiveRegion();
        }
    }
}
